<template>
  <div>
    <el-card>
      <div slot="header">
        <el-page-header @back="goBack" content="优惠券添加"></el-page-header>
      </div>
      <div>
        <el-form ref="form" :model="formData" label-width="7rem">
          <el-form-item label="优惠券名称">
            <el-input v-model="formData.name"></el-input>
          </el-form-item>
<!--          <div class="out">-->
<!--            <el-form-item label="发放日期">-->
<!--              <el-date-picker-->
<!--              class="my-date-picker"-->
<!--                v-model="formData.issueDate"-->
<!--                type="datetime"-->
<!--                placeholder="datetime"-->
<!--              ></el-date-picker>-->
<!--            </el-form-item>-->
<!--          </div>-->
          <el-form-item label="使用日期">
            <div class="out">
              <el-date-picker
                  v-model="formData.startDateTime"
                  type="datetime"
                  placeholder="选择开始日期"
                  value-format="yyyy/MM/dd HH:mm"
              >
              </el-date-picker>
            </div>
          </el-form-item>
          <el-form-item label="结束日期">
            <div class="out">
              <el-date-picker
                  v-model="formData.endDateTime"
                  type="datetime"
                  placeholder="选择结束日期"
                  value-format="yyyy/MM/dd HH:mm"
              >
              </el-date-picker>
            </div>
          </el-form-item>
          <div class="out2">
            <el-form-item label="发放数量">
              <el-radio-group v-model="total">
                <el-radio label="无限" value="无限"></el-radio>
                <el-radio label="有限" value="有限">
                  <span v-if="total_show_input">
                    <el-input
                      v-model="formData.total"
                      placeholder="请输入数量"
                      size="small"
                    ></el-input>
                  </span>
                  <span v-else>有限</span>
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </div>
          <el-form-item label="类型">
            <div class="multi-item">
              <el-select v-model="formData.type" placeholder="请选择">
                <el-option label="有价券" value="有价券"> </el-option>
                <el-option label="免费券" value="免费券"> </el-option>
              </el-select>
              <span class="letter">满</span
              ><el-input v-model="formData.threshold" placeholder=""></el-input
              ><span class="letter">减</span
              ><el-input v-model="formData.discount" placeholder=""></el-input>
            </div>
          </el-form-item>
          <el-form-item label="售价" v-if="formData.type == '有价券'">
            <el-input v-model="formData.price"></el-input>
          </el-form-item>
          <el-form-item>
            <el-row :gutter="10">
              <el-col :span="6" :offset="9">
                <el-button type="primary" class="m-el-button" @click="add"
                  >添加</el-button
                >
              </el-col>
            </el-row>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script scoped>

import { mapState } from 'vuex';
import maxois from '../assets/fake_axios'
export default {
  watch:{
  
  },
  created() {},
  computed: {
    ...mapState(['db_coupon','db_park']),
    total_show_input(){
      return this.total=='有限'
    }
  },
  methods: {
    add(){
      this.formData.issuer = '平台'
      this.formData.id = this.db_coupon[this.db_coupon.length-1]?this.db_coupon[this.db_coupon.length-1].id+1:0
      maxois.addItem.bind(this)('','','')
      .then((res)=>{
        this.$store.commit('insert_db_coupon',this.formData)
        this.$message.success('添加成功')
        this.$router.push('coupon-show')
      }).catch(err=>{
        this.$message('添加失败')
      })
    },
    saveChoosenItems(){
      this.formData.choosenItems = this.choosenItems
      this.dialogVisible = false
    },
    handleSelectionChange(val){
      //把数据转存到 data.choosenItems
      this.choosenItems = val
    },
    addRange(){
      this.dialogVisible = true
    },
    goBack() {
      this.$router.go(-1);
    },
  },
  data() {
    return {
      choosenItems:[],
      dialogVisible:false,
      total:"",  //记录是不是有限或者无限
      //发起一次请求获得已经上架的车位的情况
      tableData: [
        {
          id: "0001",
          explain: "东区热销更新",
          type: "普通销售",
          state: "未开始",
          parking: ["A001", "A002", "A003"],
        },
      ],
      formData: {
        name: "",
        // issueDate: "",
        startDateTime: "",
        endDateTime: "",
        total: 0,
        choosenList:"",
        type: "免费券",
        range: "全场通用",
        threshold: 0,
        discount: 0,
        price: 0,
      },
    };
  },
};
</script>

<style scoped>
.letter {
  margin: 0px 2rem;
}

.el-card {
  margin-top: 2rem;
}
.multi-item{
  padding-left: 7rem;
}
.el-form {
  width: 80%;
  margin: auto;
}
.el-input {
  width: 80%;
}
.el-tran >>> .el-button:first-child {
  margin-bottom: 0;
}
.multi-item {
  display: flex;
}
.multi-item .el-input {
  width: 4rem;
}


.out >>> .el-date-editor{

 width: 80%;
}
</style>